package com.gqzm.side.common.result;

import lombok.Getter;
import lombok.Setter;

/**
 * @ClassName: {@link BizException}
 * @description: 业务异常类
 * @author: peter qin
 * @date: 2023 07 14
 * @version: 1.0
 **/
@Getter
@Setter
public class BizException extends RuntimeException {

    private String errorCode;
    private String message;

    public BizException(String errorCode, String message) {
        super();
        this.errorCode = errorCode;
        this.message = message;
    }

    /**
     * 抛出业务异常
     *
     * @param errorCode
     * @param message
     */
    public static void throwIt(String errorCode, String message) {
        throw new BizException(
                errorCode,
                message);
    }

    /**
     * 抛出业务异常
     *
     * @param errorCode 错误码枚举
     */
    public static void throwIt(ErrorCode errorCode) {
        if (errorCode == ErrorCode.SUCCESS) {
            return;
        }
        throwIt(errorCode.getErrorCode(), errorCode.getMessage());
    }


    public static BizException of(ErrorCode errorCode) {
        return new BizException(
                errorCode.getErrorCode(), errorCode.getMessage());
    }


    public static BizException of(String errorCode, String message) {
        return new BizException(
                errorCode, message);
    }


    public static BizException of(ErrorCode errorCode, String message) {
        return new BizException(
                errorCode.getErrorCode(), message);
    }
}
